<template>
  <div class="container">
    <div class="head" @click="goToBest">TITIKA ACTIVE</div>
    <div class="tit">内部优惠券</div>
    <div class="gobest" v-if="gobestShow" @click="goToBest">点击进入店铺首页</div>
    <div class="coupon-list">
      <div class="no-coupon" v-if="shopCoupon.length === 0">暂无优惠券</div>
      <div class="item" v-for="(item, index) in shopCoupon" :key="index">
        <div class="left">
          <!-- 店铺优惠券 -->
          <h3>¥<em>{{item.Price}}</em> {{item.CouponName}}</h3>
          <div class="dis">
            <p v-if="item.OrderAmount">订单满{{item.OrderAmount}}使用</p>
            <p v-else>订单金额不限制</p>
            <p>使用期限:{{item.StartTime}}-{{item.EndTime}}</p>
          </div>
        </div>
        <div class="right" :class="{exist: item.exist}">
          <div class="btn-get" v-if="isISP==='true' || isBelongMan==='true'" @click="share(item.CouponId)">分享</div>
          <div class="btn-get" v-if="isISP!=='true'" @click="userGetCoupon(item.CouponId)">立即领取</div>
        </div>
      </div>
    </div>

    <!-- 分享展示层 -->
    <div class="share-banner" v-show="shareBannerShow">
      <div class="banner" id="shareDom">
        <img class="imglogo" src='../../assets/img/share-csyys-tit.jpg' />
        <img class="imglogo" src='../../assets/img/share-csyys-img.jpg' />
        <!-- <img v-if="!isISP" class="imgpro" :src="shopInfo.ShopImages" />
        <img v-else class="imgpro" src="../../assets/img/csyys.jpg" /> -->
        <div class="qrcode-dis">
          <div class="qrcode" id="qrcode"></div>
          <div class="dis">
            <img v-if="isISP" src='../../assets/img/share-csyys-txt.jpg' />
            <img v-else src='../../assets/img/share-csyys-txt2.jpg' />
          </div>
        </div>
        <!-- <div class="entrybtn">
          长按即刻进入
        </div> -->
      </div>
      <div id="shareRet">
        <van-loading 
          v-if="!shareImg" 
          class="share-loading" 
          size="24px" 
          vertical
        >
          关联码生成中...
        </van-loading>
        <img v-if="shareImg" :src='shareImg' />
      </div>
      <div class="share-btns">
        <div class="item item-1" @touchstart.prevent="press">
          <div class="img"></div>
          <!-- <img src="../../assets/img/icon-hb.png" /> -->
          <h3>长按上图保存</h3>
        </div>
        <div class="item item-2" id="copyBtn">
          <div class="img"></div>
          <!-- <img src="../../assets/img/icon-link.png" /> -->
          <h3>复制链接</h3>
        </div>
      </div>
      <div class="close" @click="closeShare">&times;</div>
    </div>
  </div>
</template>

<script>
import { getUserCoupon, getShopCoupon, userGetShopCoupon } from '../../network/coupon'
import { Toast } from 'vant'
import { drawHtml, copyInit } from '../../utils/share'


// 是否登录
// 是否为特殊运营商
// 是否为分享出来的单张券

export default {
  data() {
    return {
      gobestShow: false,
      userKey: localStorage.getItem('userKey'),
      shopCoupon: [],
      userCoupon: [],
      userId: null,
      shareCoupons: [],
      shareBannerShow: false,
      shareImg: '',
      isBelongMan: localStorage.getItem('isBelongMan'),
      isISP: localStorage.getItem('isISP'),
    }
  },
  async mounted() {
    if(this.$route.query.id){
      this.shareCoupons = [this.$route.query.id]
    }
    this.getShopCoupon()
    if(this.userKey){
      this.userId = localStorage.getItem('userId')
      await this.getCoupon()
    }
  },
  methods: {
    press(e){},
    async share(id){
      sessionStorage.setItem('shareCouponId', id)
      copyInit('coupon') // 复制链接
      
      this.shareBannerShow = true
      if(this.shareImg === ''){
        this.shareImg = await drawHtml(this, '#shareDom', '#shareRet', 'coupon')
      }
    },
    closeShare(){
      this.shareBannerShow = false
    },

    goToBest(){
      if(this.userId == 699){
        this.$router.push('/belong')
      } else {
        this.$router.push('/best')
      }
    },
    getUserCoupon(cb){
      let requestData = {
        userkey: this.userKey
      }
      getUserCoupon(requestData, list => {
        this.userCoupon = list
        cb && cb(list)
      })
    },
    getShopCoupon(cb){
      let requestData = {
        shopId: 1
      }
      getShopCoupon(requestData, list => {
        list = list.filter(item => {
          let couponIn = item.CouponName.indexOf('内部优惠券') !== -1
          return couponIn
        })
        if(this.shareCoupons.length){
          list = list.filter(item => {
            let isShareId = item.CouponId == this.shareCoupons[0]
            return isShareId
          })
        }
        
        this.shopCoupon = list
        cb && cb(list)
      })
    },
    getCoupon(){
      return new Promise(resolve => {
        this.getUserCoupon(userList => {
          this.getShopCoupon(shopList => {
            this.shopCoupon = shopList.map(item => {
              let result = userList.some(itemIn => {
                return item.CouponId === itemIn.CouponId
              })
              item.exist = result
              return item
            })
            console.log(this.shopCoupon)
            resolve()
          })
        })
      })
    },
    userGetCoupon(couponId){
      sessionStorage.setItem('goAfterLogin','coupon')
      sessionStorage.setItem('shareCoupons',this.$route.query.id)
      let requestData = {
        userkey: this.userKey,
        vshopId: 1,
        couponId,
      }
      userGetShopCoupon(requestData, async res => {
        Toast('领取成功')
        setTimeout(() => {
          this.gobestShow = true
        }, 2000)
        await this.getCoupon()
      })
    },
  },

}
</script>

<style scoped>
.container{
  background-color: #fff;
}
.head{
  color: #fff;
  line-height: 50px;
  padding-left: 20px;
  background-color: #000;
}
.tit{
  text-align: center;
  line-height: 4;
  margin-top: 10px;
}
.coupon-list{
  padding: 15px;
}
.coupon-tit{
  text-align: center;
  line-height: 50px;
  padding-top: 5px;
}
.coupon-list .item{
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 14px;
  color: #f86930
}
.coupon-list .item .left{
  flex-grow: 1;
  padding: 10px;
  border-radius: 8px;
  background-color: #ffe8df;
}
.coupon-list .item .left h3 em{
  font-size: 32px;
  font-weight: bold;
  padding: 0 5px;
}
.coupon-list .item .left .dis{
  font-size: 12px;
}
.coupon-list .item .right{
  display: flex;
  flex-wrap: wrap;
  padding: 10px 0;
  box-sizing: border-box;
  justify-content: center;
  align-items: center;
  width: 100px;
  border-left: 1px dashed #ffc6af;
  flex-shrink: 0;
  background-color: #ffe8df;
  border-radius: 8px;
}
.coupon-list .item .right.exist{
  background-image: url("../../assets/img/exist.png");
  background-position: right bottom;
  background-size: 50px auto;
  background-repeat: no-repeat;
}
.coupon-list .item .right .btn-get:first-child{
  background-color: #222;
}
.coupon-list .item .right .btn-get{
  color: #ffffff;
  width: 70px;
  padding: 6px 0;
  text-align: center;
  border-radius: 80px;
  font-size: 12px;
  background-color: #f86930;
}
.no-coupon{
  line-height: 3;
  text-align: center;
  font-size: 16px;
  padding: 20px;
  color: #999;
}
.share-banner{
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  box-sizing: border-box;
  background-color: #fff;
  z-index: 1000;
}
.share-banner .banner{
  position: relative;
  padding: 40px 20px 20px 20px;
  overflow: hidden;
}
.share-banner .banner .imglogo{
  display: block;
  width: 100%;
  margin: 0 auto;
}
.share-banner .banner .imgpro{
  width: 100%;
  background-color: #f4f4f4;
}
.share-banner .banner .pro-name{
  font-weight: bold;
  line-height: 1.4;
  padding-top: 30px;
}
.share-banner .banner .pro-tips{
  color: #999999;
  font-size: 14px;
  padding: 10px 0 0 0;
}
.share-banner .share-btns{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  z-index: 1001;
}
.share-banner .share-btns .item{
  text-align: center;
  /* flex-grow: 1; */
  width: 50%;
}
.share-banner .share-btns .item .img{
  display: inline-block;
  width: 35px;
  height: 35px;
  margin-bottom: 5px;
  background: url(../../assets/img/icon-hb.png);
  background-size: 100% auto;
}
.share-banner .share-btns .item-2 .img{
  background: url(../../assets/img/icon-link.png);
  background-size: 100% auto;
}
.share-banner .qrcode-dis{
  display: flex;
  align-items: flex-end;
  padding-top: 20px;
}
.share-banner .qrcode-dis .qrcode{
  width: 74px;
  height: 74px;
  background-color: #fff;
  box-sizing: border-box;
}
.share-banner .qrcode-dis .dis{
  color: #999;
  font-size: 14px;
  flex-grow: 1;
  text-align: right;
  font-size: 16px;
  line-height: 1.8;
  color: #000;
}
.share-banner .qrcode-dis .dis img{
  width: 110px;
}
.share-banner .entrybtn{
  margin-top: 20px;
  line-height: 50px;
  font-size: 14px;
  color: #fff;
  text-align: center;
  background-color: #000;
}
.share-banner #shareRet{
  /* display: none; */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  z-index: 20;
  background-color: #fff;
}
/* .share-banner #shareRet::-webkit-scrollbar {
  width: 0;
  height: 0;
  -webkit-overflow-scrolling: touch;
  -ms-overflow-style: none;
  overflow: -moz-scrollbars-none;
  background-color: rgba(240,240,240,0)
} */
.share-banner .share-loading{
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -20px 0 0 -40px;
}
.share-banner #shareRet img{
  width: 100%;
  vertical-align: baseline;
}
.close{
  position: absolute;
  top: 12px;
  right: 13px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  text-align: center;
  line-height: 22px;
  color: #fff;
  font-size: 20px;
  z-index: 21;
  background-color: #000;
}
.arrow{
  width: 8px;
  height: 8px;
  border-radius: 2px;
  border-right: 2px solid #fff;
  border-top: 2px solid #fff;
  position: relative;
  transform: rotate(45deg);
}
.gobest{
  margin: 10px 15px;
  color: #fff;
  line-height: 45px;
  border-radius: 5px;
  text-align: center;
  background: #f86930;
}
</style>
<style>
.share-banner #shareRet canvas{
  margin-left: -900px;
}
</style>